﻿using System;
using System.Web.UI.WebControls;
using Alserve.Ffl;
using Alserve.Ffl.Data;

public partial class ffl_user_webusercontrols_showmyteam : System.Web.UI.UserControl
{
    

    public event EventHandler ControlRefreshed;

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UpdatePositionCounts();
            UpdateShowMyTeamDataSource();
        }
    }
    protected void ShowMyTeamRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image playerImage = (Image)e.Item.FindControl("PlayerImage");
            Int32 espnRef = Convert.ToInt32(((HiddenField)e.Item.FindControl("EspnRefHidden")).Value);
            Int32 clubId = Convert.ToInt32(((HiddenField)e.Item.FindControl("ClubIdHidden")).Value);
            playerImage.ImageUrl = FflCommon.ImageLocation(espnRef, clubId);
        }

    }

    protected void ShowMyTeamRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Button removePlayerButton = (Button)e.Item.FindControl("RemovePlayerButton");
        removePlayerButton.Enabled = false;

        UserPlayer thisUserPlayer = new UserPlayer();
        thisUserPlayer.Where.UserId.Value = Convert.ToInt32(Session["UserId"]);
        thisUserPlayer.Where.PlayerId.Value = Convert.ToInt32(((HiddenField)e.Item.FindControl("PlayerIdHidden")).Value);
        if (thisUserPlayer.Query.Load())
        {
            thisUserPlayer.MarkAsDeleted();
            thisUserPlayer.Save();
        }

        UpdateShowMyTeamDataSource();
        UpdatePositionCounts();
        if (this.ControlRefreshed != null)
        {
            this.ControlRefreshed(this, new EventArgs());
        }
    }

    public void UpdateShowMyTeamDataSource()
    {
        _UserPlayerSquad thisSquad = new _UserPlayerSquad();
        thisSquad.Where.UserId.Value = Convert.ToInt32(Session["UserId"]);
        if (thisSquad.Query.Load())
        {
            ShowMyTeamRepeater.DataSource = thisSquad.DefaultView;
            ShowMyTeamRepeater.DataBind();
        }
    }

    public void UpdatePositionCounts()
    {
        Int32[] positionCount = new Int32[5];

        UserPlayerScores thisUserPlayer = new UserPlayerScores();
        thisUserPlayer.Where.UserId.Value = Convert.ToInt32(Session["UserId"]);

        if (thisUserPlayer.Query.Load())
        {
            while (!thisUserPlayer.EOF)
            {
                positionCount[thisUserPlayer.PositionId]++;
                thisUserPlayer.MoveNext();
            }
        }

        GoalkeeperCountLabel.Text = positionCount[1].ToString() + "/2";
        DefenderCountLabel.Text = positionCount[2].ToString() + "/7";
        MidfielderCountLabel.Text = positionCount[3].ToString() + "/6";
        ForwardCountLabel.Text = positionCount[4].ToString() + "/5";
    }
}
